package com.molichuxing.services.util;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.ehcache.EhCacheCacheManager;

import java.io.*;

public class FileEhCacheUtil {

    private EhCacheCacheManager ehCacheCacheManager;

    public EhCacheCacheManager getEhCacheCacheManager() {
        return ehCacheCacheManager;
    }

    public void setEhCacheCacheManager(EhCacheCacheManager ehCacheCacheManager) {
        this.ehCacheCacheManager = ehCacheCacheManager;
    }

    public void initContractCache() {
        /*// 一次性付款合同
        String contractTemplateFilePath = CONTRACT_FULL_TEMPLATE_FILE_PATH;
        byte[] stream = file2byte(contractTemplateFilePath);
        Element element = new Element("FullContractTemplate", stream);
        getFileCache().put(element);

        // 分期付款合同
        contractTemplateFilePath = CONTRACT_INSTALLMENT_TEMPLATE_FILE_PATH;
        stream = file2byte(contractTemplateFilePath);
        element = new Element("InstallmentContractTemplate", stream);
        getFileCache().put(element);

        // 补充协议
        contractTemplateFilePath = CONTRACT_SUPPLEMENTARY_TEMPLATE_FILE_PATH;
        stream = file2byte(contractTemplateFilePath);
        element = new Element("SupplementaryContractTemplate", stream);
        getFileCache().put(element);
        // 补充协议(一次性付款版本)
        contractTemplateFilePath = CONTRACT_SUPPLEMENTARYSUPPLY_TEMPLATE_FILE_PATH;
        stream = file2byte(contractTemplateFilePath);
        element = new Element("SupplementarySupplyContractTemplate", stream);
        getFileCache().put(element);
        // 担保合同
        contractTemplateFilePath = CONTRACT_GUARANTEE_TEMPLATE_FILE_PATH;
        stream = file2byte(contractTemplateFilePath);
        element = new Element("GuaranteeContractTemplate", stream);
        getFileCache().put(element);

        // 交车单
        contractTemplateFilePath = GIVE_CAR_TEMPLATE_FILE_PATH;
        stream = file2byte(contractTemplateFilePath);
        element = new Element("SignGiveCarTemplate", stream);
        getFileCache().put(element);*/
    }

    private Cache getFileCache() {
        CacheManager cacheManager = ehCacheCacheManager.getCacheManager();
        return cacheManager.getCache("FileCache");
    }

    /**
     * 分期合同
     *
     * @return
     */
    public byte[] getInstallmentContractTemplate() {
        return ObjectToByte(getFileCache().get("InstallmentContractTemplate").getObjectValue());
    }

    /**
     * 一次性付款合同
     *
     * @return
     */
    public byte[] getFullContractTemplate() {
        return ObjectToByte(getFileCache().get("FullContractTemplate").getObjectValue());
    }

    /**
     * 补充协议
     *
     * @return
     */
    public byte[] getSupplementaryContractTemplate() {
        return ObjectToByte(getFileCache().get("SupplementaryContractTemplate").getObjectValue());
    }

    /**
     * 补充协议(一次性付款版本)
     *
     * @return
     */
    public byte[] getSupplementarySupplyContractTemplate() {
        return ObjectToByte(getFileCache().get("SupplementarySupplyContractTemplate").getObjectValue());
    }

    /**
     * 担保合同
     *
     * @return
     */
    public byte[] getGuaranteeContractTemplate() {
        return ObjectToByte(getFileCache().get("GuaranteeContractTemplate").getObjectValue());
    }

    /**
     * 交车单
     *
     * @return
     */
    public byte[] getSignGiveCarTemplate() {
        return ObjectToByte(getFileCache().get("SignGiveCarTemplate").getObjectValue());
    }

    /**
     * 根据模板名称加载模板和模板路径
     *
     * @param templateName 模板名称
     * @return
     */
    public byte[] getTemplate(String templateName, String templatePath) {
        // 先判断缓存中是否存在该模板
        byte[] template = null;
        String key = templateName + templatePath;
        Element templateElement = getFileCache().get(key);
        if (templateElement == null) {
            // 不存在，生成并放入缓存
            template = file2byte(templatePath);
            Element element = new Element(key, template);
            getFileCache().put(element);
        } else {
            // 存在，取出
            template = ObjectToByte(templateElement.getObjectValue());
        }
        return template;
    }


    public static byte[] ObjectToByte(Object obj) {
        byte[] bytes = null;
        try {
            // object to bytearray
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);

            bytes = bo.toByteArray();

            bo.close();
            oo.close();
        } catch (Exception e) {
            System.out.println("translation" + e.getMessage());
            e.printStackTrace();
        }
        return bytes;
    }

    public static byte[] file2byte(String filePath) {
        byte[] buffer = null;
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            File file = new File(filePath);
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return buffer;
    }

}
